// views/index/school/ranking/index.js
var BASE_URL = getApp().globalData.BASE_URL

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    active: 0,
    schoolList: "",
    option: [],
    value: 0,
    showloading:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSchoollist(3, 5)
  },
  //获取通过专业排序学校列表
  getSchoollist: function (id, pid) {
    var _this = this
    wx.request({
      url: `${BASE_URL}/school/toplistsub/id/${id}/pid/${pid}.html`,
      method: "POST",
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        res.data.paiming.forEach(function (item, index) {
          item.img=`${BASE_URL}/uploads/school/xuexiaotupian/` + res.data.paiming[index].img.split(",")[0]
        })
        var optionCopy = []
        res.data.list.forEach(function (item, index) {
          var arr = {
            text: item.name,
            value: item.id
          }
          optionCopy.push(arr)
        })
        _this.setData({
          id: id,
          option: optionCopy,
          value: res.data.list[0].id,
          schoolList: res.data.paiming,
          showloading:false
        })
      }
    })

  },
  //获取通过世界排序学校列表
  getotherSchoollist: function (id) {
    var _this = this
    wx.request({
      url: `${BASE_URL}/school/toplist/id/${id}.html`,
      method: "POST",
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)

        res.data.forEach(function (item, index) {
          item.img=`${BASE_URL}/uploads/school/xuexiaotupian/` + res.data[index].img.split(",")[0]
        })
        _this.setData({
          schoolList: res.data
        })
      }
    })
  },

  onChange(event) {
    console.log(event.detail.title)
    if (event.detail.title == '德国经济周刊专业排名') {
      this.getSchoollist(4, 6)
    }
    if (event.detail.title == 'CHE德国高等教育发展中心大学排名') {
      this.getSchoollist(3, 5)
    }
    if (event.detail.title == 'QS世界大学排名') {
      this.getotherSchoollist(1)
    }
    if (event.detail.title == '泰晤士世界大学排名') {
      this.getotherSchoollist(2)
    }
  },
  onToggle: function (e) {
    console.log(e)
    var _this = this
    wx.request({
      url: `${BASE_URL}/school/toplistsub/id/${_this.data.id}/pid/${e.detail}.html`,
      method: "POST",
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        res.data.paiming.forEach(function (item, index) {
          item.img=`${BASE_URL}/uploads/school/xuexiaotupian/` + res.data.paiming[index].img.split(",")[0]
        })
        _this.setData({
          schoolList: res.data.paiming
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onShareAppMessage: function () {

  },
//跳学校详情页页
schoolDetail: function (e) {
    var id = e.currentTarget.dataset.id 
  console.log(id)
    wx.navigateTo({
      url: `/views/index/school/schooldetail/index?id=${id}`
    })
  },
})